package pl.prv.polanie.client.widgets.schedule;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dto.CSSubjectEventDTO;
import pl.prv.polanie.client.dto.CSVSubjectEventDTO;
import pl.prv.polanie.client.model.CacheService;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.widgets.layout.tooltip.ToolTip;
import pl.prv.polanie.client.widgets.utils.Borders;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.Widget;

/**
 * Klasa uzywana do pokazywania na podziale zajec gdzie jeszcze mozna wrzucic SubjectEventy.
 * @author Janusz Prokulewicz
 * Created: Jun 4, 2008
 */

public class LocationForSubjectEvent extends Composite {
	
	private static final String STYLE_LOCATION_FOR_SUBJECT_EVENT = "my-LocationForSubjectEvent";
	
	private static final String STYLE_EVENT_PROPERTY = "my-Event-property";
	private static final String STYLE_EVENT_PROPERTY_RIGHT = "my-Event-property-right";
	private static final String STYLE_EVENT_DETAILS = "my-Event-details";
	
	//user can catch for it and start dragging
	private final HTML handlerHTML = new HTML("");
	
	//private static final HTML 
	
	private boolean wasAttached = false;
	
	/**
	 * wskazuje czy zajecia odbywaja sie:
	 * 	co tydzien = 0
	 * 	w tygodnie parzyste = 1
	 *  w tygodnie nieparzyste = 2
	 */ 
//	private int allOrEvenOrOdd = 0;
	
	
	private AbsolutePanel mainAP = new AbsolutePanel();
	
	private CSSubjectEventDTO csSubjectEventDTO = null;
	private CSVSubjectEventDTO csvSubjectEventDTO = null;
	
	private HTML eventProperty1HTML;  
	private HTML eventProperty2HTML;  
	private HTML eventProperty3HTML;
	
	//wspolrzedna subject eventa na formatce dnia 
	private int y; 
	
	
	private MouseListener mouseListenerAdapter = new MouseListenerAdapter(){
		public void onMouseMove(Widget sender, int x, int y) {
//			Planner.log("onMouseMove");
			if (!Planner.isToolTipDisplayed()) {
				ToolTip tip = new ToolTip(csvSubjectEventDTO);
				Planner.displayToolTip(tip, x + sender.getAbsoluteLeft() + 10, y + sender.getAbsoluteTop() + 10);	
			} else {
				Planner.setToolTipPosition(x + sender.getAbsoluteLeft() + 10, y + sender.getAbsoluteTop() + 10);
			}
		}
		public void onMouseLeave(Widget sender) {
			Planner.hideToolTip();
//			Planner.log("handler.onMoulseLeave");
		}
	};
	
	public LocationForSubjectEvent(PeriodOfTime pot) {
		this.y = ScheduleUtil.convertFromStartTimeToPixels((int)pot.getStartTime(), CacheService.isFullTimeSemesterSelected());
		
		initWidget(mainAP);
		setupMainAP();

		setStyleName(STYLE_LOCATION_FOR_SUBJECT_EVENT);
		Borders.addBlackDashedBorders(mainAP);
		
		int height = ScheduleUtil.convertFromDurationToPixels((int)pot.getDuration(), CacheService.isFullTimeSemesterSelected()); 
		this.setHeight(height + "px");
	}
	
//	public LocationForSubjectEvent(CSSubjectEventDTO dto) {
//		this.csSubjectEventDTO = dto;
//		this.y = ScheduleUtil.convertFromStartTimeToPixels(dto.getStartTime().intValue());
//		
//		CSVSubjectEventDTO csvSE = ViewDataProvider.getCSVSubjectEvent(dto);
//		this.csvSubjectEventDTO = csvSE;
//		setupEventPropertiesHTML(csvSE);
////		setupHandlerHTML();
//		setupMainAP();
//
//		int height = ScheduleUtil.convertFromDurationToPixels(dto.getDuration().intValue());
////		Planner.log("LecturerPreferences.kostr() duration: " + dto.getDuration().intValue() + ", height " + height);
//		setHeight(height + "px");
//
//		
//		initWidget(mainAP);
//		setStyleName(STYLE_LOCATION_FOR_SUBJECT_EVENT);
//		Borders.addGrayDashedBorders(mainAP);
//	}

	private void setupEventPropertiesHTML(CSVSubjectEventDTO e) {
		eventProperty1HTML = new HTML(e.getSubject());
		eventProperty1HTML.setStyleName(STYLE_EVENT_PROPERTY);
		eventProperty2HTML = new HTML(e.getRoom());
		eventProperty2HTML.setStyleName(STYLE_EVENT_PROPERTY);
		eventProperty3HTML = new HTML(e.getLecturer());
		eventProperty3HTML.setStyleName(STYLE_EVENT_PROPERTY);
	}
	
//	private void setupHandlerHTML() {
//		handlerHTML.setStyleName(STYLE_EVENT_HANDLER);
//		handlerHTML.setWidth(200+"px");
//		handlerHTML.setHeight(54+"px");
//		handlerHTML.addMouseListener(mouseListenerAdapter);
////		handlerHTML.addClickListener(clickListenerAdapter);
//	}
	
	private void setupMainAP() {
		mainAP.setWidth("200px");
//		mainAP.setHeight("54px");
		
//		mainAP.add(eventProperty1HTML,-1,-1);
//		mainAP.add(eventProperty2HTML,-1,-1);
//		mainAP.add(eventProperty3HTML,-1,-1);
		
		//mainAP.add(eventDetailsHTML);
		
		mainAP.add(handlerHTML, 0, 0);
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
//	@Override
	public void setHeight(String height) {
		mainAP.setHeight(height);
		handlerHTML.setHeight(height);
	}
	
	

}
